<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="箱号" prop="box_id">
        <el-input
          v-model="queryParams.box_id"
          placeholder="请输入箱号"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="拆分状态" prop="split_status">
        <el-select v-model="queryParams.split_status" placeholder="拆分状态" clearable size="small">
          <el-option label="未拆分" value="0" />
          <el-option label="已拆分" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="list">
      <el-table-column type="selection" align="center" width="50" />
      <el-table-column prop="name" label="库位名称" align="center" />
      <el-table-column prop="tray_id" label="托盘码" align="center" width="200" />
      <el-table-column prop="box_id" label="箱号" align="center" width="200" />
      <el-table-column prop="Good_Qty" label="Good_Qty" align="center" />
      <el-table-column prop="Hold_Qty" label="Hold_Qty" align="center" />
      <el-table-column prop="NG_Qty" label="NG_Qty" align="center" />
      <el-table-column prop="out_number" label="出库数量" align="center" />
      <el-table-column prop="status" label="拆分状态" align="center">
        <template v-slot="scope">
          <span v-if="scope.row.split_status === '0'">未拆分</span>
          <span v-else-if="scope.row.split_status === '1'">已拆分</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="出库状态" align="center">
        <template v-slot="scope">
          <span v-if="scope.row.status === '0'">未出库</span>
          <span v-else-if="scope.row.status === '1'">全部出库</span>
          <span v-else-if="scope.row.status === '2'">部分出库</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250">
        <template slot-scope="scope">
          <el-button
            v-show="isShow(scope.row)"
            v-if="scope.row.split_status === '0'"
            size="mini"
            type="success"
            @click="slice(scope.row,2)"
          >拆箱
          </el-button>
          <el-button
            v-if="scope.row.split_status === '1'"
            size="mini"
            type="info"
            @click="slice(scope.row,1)"
          >详情
          </el-button>
          <el-button
            v-show="isShow(scope.row)"
            size="mini"
            type="primary"
            @click="handleEdit(scope.row)"
          >修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.limit" @pagination="getList" />
    <!-- 新增/修改 -->
    <AddEdit :open.sync="open" :title="title" :form="form" :select-data="selectData" @getList="getList" />
  </div>
</template>
<script>
import { boxList } from '@/api/stock'
import AddEdit from './AddEdit'
export default {
  name: 'Index',
  components: {
    AddEdit
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      open: false,
      title: '',
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      form: {},
      selectData: [],
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        box_id: undefined,
        split_status: undefined
      },
      show: false,
      ids: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      const that = this
      this.loading = true
      this.queryParams['tray_id'] = this.$route.query.tray_id
      boxList(this.queryParams).then(response => {
        console.log(response)
        if (response.code === 10000) {
          that.list = response.data
          that.total = response.count
          that.loading = false
        }
      })
    },
    /** 新增按钮操作 */
    handleAdd() {
    },
    /** 编辑按钮操作 */
    handleEdit(row) {
      this.form = row
      this.open = true
      this.title = '编辑'
    },
    /** 删除按钮操作 */
    handleDel(row) {
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        page: 1,
        limit: 10,
        box_id: undefined,
        split_status: undefined
      }
      this.handleQuery()
    },
    /** 拆箱 */
    slice(row, type) {
      const that = this
      if (type === 2) {
        that.$confirm('确认拆箱吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          that.loading = true
          that.$router.push({
            path: '/stock/slice',
            query: { box_id: row.box_id }
          })
        }).catch(() => {
        })
      } else {
        that.$router.push({
          path: '/stock/slice',
          query: { box_id: row.box_id }
        })
      }
    },
    /** 按钮显示判断 */
    isShow(row) {
      if (row.split_status === '0') {
        if (row.status > 0) {
          return false
        } else {
          return true
        }
      } else if (row.split_status === '1') {
        return false
      }
    }
  }
}
</script>

